﻿Set fso = CreateObject("Scripting.FileSystemObject")
ProjectDir = fso.getparentfoldername(Environment.Value("TestDir"))
ProjectDir = ProjectDir+"\"
Set fso = nothing
TestSetExcelFile = ProjectDir&"testSets.xls"   '管理所有测试用例的Excel文件的路径
testScriptFolderName =ProjectDir&"testScript" 'task脚本文件所在的根目录
testDataFolderName = ProjectDir&"testData"'测试数据文件所在的根目录
Environment("ProjectDir")  = ProjectDir
Environment("TestSetExcelFile")  = TestSetExcelFile
Environment("testScriptFolderName")  = testScriptFolderName
Environment("testDataFolderName")  = testDataFolderName
Environment("Log_Dir")  = ProjectDir&"\logs"
Environment("URL")="URL"
Environment("TCase") = "testCase"
Environment("Biz") = "Biz"
Environment("Task") = "Task"
Environment("Row") = 11
'Environment("numSuccess")  = 0
'Environment("numErrors")  = 0
'Environment("bResult") = "PASS"

 'Msgbox "请关闭相关的Excel表！"
Set TestScriptObject = CreateObject("Scripting.Dictionary")

Dim fso, ofile
Set fso = CreateObject("Scripting.FileSystemObject")
If  fso.fileexists(Environment("ProjectDir")&"result\"&" TestReport"& Date&".xls")  Then
	fso.deletefile(Environment("ProjectDir")&"result\"&" TestReport"& Date&".xls")
End If


'xmlLogPrint "<?xml version=""1.0"" encoding=""GB2312""?>"
'*******************************************以上类的声明和全局变量 的定义************************************************************************
  
Set objtestProject = New TestProject                                               '构造一个自动化测试项目 
objtestProject.StartTestProject  "testSets.xls" 
Call ExcelReportWrite(excelLog)
Set objtestProject = nothing
Set TestScriptObject = nothing
 '******************************************TestProject类，这个类中很多个测试 用例集（TestSets）******************************************************
Class TestProject   

	Public Function StartTestProject (ExcelName)   '这里传入一个excel表格的路径。
		  'xmlLogPrint "<Project>"
		  Set excelLog = CreateObject("Scripting.Dictionary")
		  InitTestProject  ExcelName                                  '用传入参数作一些初始化操作
		  For i =0To testSets.Count -1                              '循环执行每个TestSet 
			   Set objTestSet = new TestSet                          '构造一个TestSet对象
				TestSetArrays = testSets.Items
				TestSetArray = TestSetArrays(i)
				Environment("TCase") = TestSetArray(0)
				'xmlLogPrint "<"&TestSetArray(0)&">"
				objTestSet.StartTestSet(TestSetArray)       '传入一个test结构,执行TestSet 
				'xmlLogPrint "<"&"/"&TestSetArray(0)&">"
				Set objTestSet = nothing
		  Next
		   'xmlLogPrint "<测试结果统计>"&"Successed"& Environment("numSuccess")&"例,"&"Failed"&Environment("numErrors")&"例!"
		   'xmlLogPrint "</测试结果统计>"
		   'xmlLogPrint "</Project>"
		  DataTable.DeleteSheet  "TestSets"
	End Function

	Private Function InitTestProject(ExcelName)
		
		 Set testSets = CreateObject("Scripting.Dictionary")
		 TestSetExcelFile = ProjectDir&ExcelName
		 MyTestSets = "TestSets"
		 DataTable.AddSheet(MyTestSets)
		 Err.Clear
		 On Error Resume Next
		 DataTable.ImportSheet TestSetExcelFile,1, MyTestSets
		 If  err.number = 0  Then
			 row_count = DataTable.GetSheet(MyTestSets).GetRowCount
			 Dim setArray(3)
			 For i = 1 To row_count
				DataTable.GetSheet(MyTestSets).SetCurrentRow(i)
				isRun = DataTable.Value("IDX", MyTestSets) 
				If isRun = "√"  Then
				   testsetName = DataTable.Value("name", MyTestSets)  
				   setArray(0)= testsetName                                                          'testSets结构中包含用例集的name列
				   setArray(1)= DataTable.Value("table", MyTestSets)      'testSets结构中包含用例集的table列
				   setArray(2)= DataTable.Value("sheet", MyTestSets)      'testSets结构中包含用例集的table列
				   If  err.number = 0 Then
					   testSets.Add testsetName, setArray 
				   else
						'Environment("bResult") = "FAIL"
						Msgbox "FAIL ERROR 检查TestSets.xls表中的字段IDX, name, table, sheet 字段是否有错."
				   End If                                 
				End If
				err.clear  	
			Next
			else  
				'Environment("bResult") = "FAIL"
				logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
				Msgbox "ERROR 检查TestSets.xls表是否有打开"
			End If
		On Error Goto 0
	End Function

Private   testSets                  

End Class
 '******************************************TestProject类，这个类中很多个测试 用例集（TestSets）******************************************************
 Class TestSet

	Public Function StartTestSet(testsetArray)
		InitTestSet testsetArray
		For i = 0 To testCases.Count -1         
			TestCaseArrays = testCases.Items
			TestCaseArray = TestCaseArrays(i)		
			If   TestCaseArray(3) = "√" Then
				''xmlLogPrint "<"&TestCaseArray(0)&">"
				Set objTestCase = new TestCase
				objTestCase.StartTestCase(TestCaseArray)
				'xmlLogPrint"<"&"/"&TestCaseArray(0)&">" 
				Set objTestCase = nothing
			End If       
		Next 
		DataTable.DeleteSheet "TestCases"
	End Function 

	Private  Function InitTestSet(testsetArray)
	
		testsetName = testsetArray(0)
		testsetTable = testsetArray(1)
		testsetSheet = testsetArray(2)
		Environment("TestSetsTable") = testsetTable
		Set testCases = CreateObject("Scripting.Dictionary")
		testsetTable = pathFind(testDataFolderName,testsetTable, "xls")'测试数据文件的绝对路径
		MyTestCases = "TestCases"
		DataTable.AddSheet(MyTestCases)
		On error Resume Next
		DataTable.ImportSheet testsetTable,1, MyTestCases'导入TestCases表  
		If    Err.Number <> 0  Then
			'Environment("bResult") = "FAIL"
			logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
			Msgbox "ERROR 请检查"&testsetTable&"是否存在或已经被打开,检查testsets.xls,中与之对应的table, sheet栏的名字"
		else
			row_count = DataTable.GetSheet(MyTestCases).GetRowCount
			tempcaseName = ""
			Dim caseArray(4)
			For i = 1 To row_count-1
				DataTable.GetSheet(MyTestCases).SetCurrentRow(i)
				'isRun = DataTable.Value("IDX", MyTestCases)
				isBiz=DataTable.Value("bizName",MyTestCases)
				If isBiz = "业务流程"  Then
					If Not tempcaseName = "" Then
						caseArray(2) = i-1   '该case结束时的行数
						testCases.Add testcaseName, caseArray
					End If 
				
					testcaseName = DataTable.Value("taskName", MyTestCases)
					Environment("Biz") = testcaseName
					tempcaseName = testcaseName
					caseArray(0) = testcaseName
					caseArray(1) = i+1  '该case开始时的行数
					caseArray(2) = row_count
					caseArray(3)= DataTable.Value("IDX", MyTestCases)
					If    err.number <> 0 Then
						'Environment("bResult") = "FAIL"
						logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
						Msgbox "检查"&testsetTable&"中IDX, bizName, description 中各字段是否有错!"
						Err.Clear
					End If
			  End If 	  
			Next

			testCases.Add tempcaseName, caseArray   '*****************************增加是否有重复的判断．

		End IF
		On error Goto 0
	End Function
			   
		  Private  testsetName
		  Private  testsetTable
		  Private  testsetSheet
		  Private  testCases
	End Class
	
	 '******************************************TestSet类，这个类中很多个业务流程（TestCases）******************************************************
	
	
	  '******************************************TestCase类，这个类中很多个Tasks******************************************************
	Class TestCase
	
	Public  Function StartTestCase(TestCaseArray)
		 
		InitTestCase TestCaseArray
		For i = 0 To testTasks.Count-1
			Set objTestTask = new TestTask
			TestTaskArrays = testTasks.Items
			TestTaskArray = TestTaskArrays(i)
			objTestTask.StartTestTask(TestTaskArray)
			'taskLogPrint TestTaskArray(1)
			Set objTestTask = nothing
		Next
				  
		 End Function
		 
	Private  Function InitTestCase(TestCaseArray)
		testcaseName = TestCaseArray(0)
		casestartRow = TestCaseArray(1)
		caseendRow   = TestCaseArray(2)
		Set testTasks = CreateObject("Scripting.Dictionary")
		MyTestTasks = "TestCases"
		Dim j
		j = 0
		Dim taskArray(4)
		On Error Resume Next
		For i = casestartRow To caseendRow
		DataTable.GetSheet(MyTestTasks).SetCurrentRow(i)
		isRun = DataTable.Value("IDX", MyTestTasks)
		   If isRun = "√" Then
			classoftesttask = DataTable.Value("bizName", MyTestTasks)
			testtaskName = DataTable.Value("taskName", MyTestTasks)
			tableOfTask = DataTable.Value("bizDataTable", MyTestTasks)
			taskFilter = DataTable.Value("filter", MyTestTasks)
			taskArray(0) = classoftesttask 
			taskArray(1) = testtaskName
			taskArray(2) = tableOfTask
			taskArray(3) = taskFilter
			If   Err.Number = 0 Then
				testTasks.Add j, taskArray
				j = j+1
			else
				'Environment("bResult") = "FAIL"
				logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
				Msgbox "请检查"&MyTestCases&"表中IDX, bizName, description, filter,taskName各字段有没有错!"
		   End If
		   Err.Clear
		End If      	
		Next
		On Error Goto 0
	
	End Function
	
	
	Private   testcaseName
	Private   casestartRow
	Private   caseendRow
	Private   testTasks
	
	End Class
	
	  '******************************************TestCase类，这个类中很多个Tasks******************************************************
	
	  '******************************************TestTask类******************************************************
Class TestTask
	Private Function InitTestTask(TestTaskArray)
		classoftesttask = TestTaskArray(0)
		testtaskName = TestTaskArray(1)
		tableOfTask  = TestTaskArray(2)
		taskFilter  = TestTaskArray(3)
	End Function
	
	Private Function ExecuteTask(taskDataSheetName, TaskDataFilePath)
		DataTable.AddSheet(taskDataSheetName)
		On Error Resume Next
		DataTable.ImportSheet TaskDataFilePath,taskDataSheetName,taskDataSheetName
		If  Err.number = 0 Then
			test_data_row_count = DataTable.GetSheet(taskDataSheetName).GetRowCount
			For j=1 To test_data_row_count
				DataTable.GetSheet(taskDataSheetName).SetCurrentRow( j )
					If Eval(generateFilterExp(taskDataSheetName,taskFilter)) Then
						str = "obj"&classoftesttask&"."&testtaskName&" "&chr(34)&taskDataSheetName&chr(34)
						Execute str
						'ErrorXmlPrint   '如果有错误则输出。
						If  err.number <> 0 Then
							'Environment("bResult") = "FAIL"
							logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
						End If
					   Err.Clear
					End If 
			Next  
			sheetName =taskDataSheetName
			DataTable.DeleteSheet  sheetName
		else
			'Environment("bResult")="FAIL"
			Msgbox "检查"&TaskDataFilePath&"中是否有表"&taskDataSheetName
		End if 
		On Error Goto 0
	End Function
			
	Public Function StartTestTask(TestTaskArray)
		InitTestTask TestTaskArray 
		'Environment("bResult") = "PASS"  '默认开始执行每一个的步骤为成功
		If Not TestScriptObject.Exists (classoftesttask)  then 
			taskFilePath = pathFind(Environment("testScriptFolderName"),classoftesttask,"vbs")
			On Error Resume Next
			ExecuteFile taskFilePath   'executefile  qtp独有的加载外部的vbs 文件.
			If Err.Number = 0 Then
				TestScriptObject.add classoftesttask,taskFilePath
				Execute "Set obj"&classoftesttask&" = new "&classoftesttask
				If   Err.Number <> 0 Then
					'Environment("bResult")="FAIL"
					msgbox "查看"&taskFilePath&"中是否有"& classoftesttask&"类"
					logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
					Err.Clear
				End If
			Else
					msgbox "请查看"&taskFilePath&"是否存在或者taskFilePath中有错误!"
			End If   
			On Error Goto 0
		End If
		Environment("Task")=testtaskName	
		'Environment("Row") = Environment("Row") +1
		If Not tableOfTask="" Then
			tempArray = split(tableOfTask,".")
			If UBound(tempArray)=0 Then
				TaskDataFilePath = pathFind(Environment("testDataFolderName"), Environment("TestSetsTable"),"xls")
				ExecuteTask tempArray(0), TaskDataFilePath
			else
				'如果是引用的外部Excel的sheet，那么。。。
				TaskDataFilePath = pathFind(Environment("testDataFolderName"),tempArray(0),"xls")		
				ExecuteTask tempArray(1), TaskDataFilePath
			End If 		
		Else
			'如果Sheet名称为空则不加载
			str = "Call obj"&classoftesttask&"."&testtaskName&"()"
			On error resume Next
			Execute str
			'ErrorXmlPrint 
			If  err.number <> 0 Then
			'Environment("bResult") = "FAIL"
			logPrint("FAIL Error Num: " & Err.Number & "; Error Src: " & Err.Source & "; Error Desc: " & Err.Description)
			End If
			On error Goto 0
			End If
			'计算PASS和FAIL的用例数
'			If   Environment("bResult") = "PASS" Then
'				Environment("numSuccess") =  Environment("numSuccess") +1
'			else 
'				Environment("numErrors") =  Environment("numErrors")+1
'		   End If			
	End Function
				   

Private classoftesttask
Private  testtaskName
Private tableOfTask
Private taskFilter

End Class

  '******************************************TestTask类******************************************************



